#!/bin/bash

SUBJECTS_DIR="/media/amd/data/long/MM"
SAV_DIR="/media/amd/data/long/parc_tables/mm_schaefer400/mm"
MAX_JOBS=50  

process_subject() {
    local subj=$1
    local lh_path="$SUBJECTS_DIR/$subj/${subj}.scheafer400.stats.lh.table"
    local rh_path="$SUBJECTS_DIR/$subj/${subj}.scheafer400.stats.rh.table"

    mri_surf2surf --srcsubject fsaverage \
        --sval-annot /media/amd/data/long/lh.Schaefer2018_400Parcels_17Networks_order.annot \
        --trgsubject $subj \
        --trgsurfval $SUBJECTS_DIR/$subj/label/lh.scheafer400.annot 
        --hemi lh

    mri_surf2surf --srcsubject fsaverage \
        --sval-annot /media/amd/data/long/rh.Schaefer2018_400Parcels_17Networks_order.annot \
        --trgsubject $subj \
        --trgsurfval $SUBJECTS_DIR/$subj/label/rh.scheafer400.annot \
        --hemi rh
        
    mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/lh.scheafer400.annot" \
        -b -f "$lh_path" "$subj" lh
    mris_anatomical_stats -a "$SUBJECTS_DIR/$subj/label/rh.scheafer400.annot" \
        -b -f "$rh_path" "$subj" rh
}

export -f process_subject
export SUBJECTS_DIR

ls "$SUBJECTS_DIR" | grep sub | parallel -j $MAX_JOBS --progress --joblog parallel_joblog.txt \
    "process_subject {}"

echo "All tasks have been completed - see parallel_joblog.txt"